/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Renderers;

import comu.Configuracio;
import comu.Utilitats;
import controlador.TractamentControler;
import entity.Abonat;
import entity.Camp;
import entity.Collita;
import entity.Produccio;
import entity.Productor;
import entity.Tractament;
import java.awt.Color;
import java.awt.Component;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 * @author David
 */
public class CellRenderer extends JLabel implements TreeCellRenderer {

    DefaultTreeCellRenderer list = new DefaultTreeCellRenderer();

    @Override
    public Component getTreeCellRendererComponent(JTree jtree, Object o, boolean isSelected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Object value = ((DefaultMutableTreeNode) o).getUserObject();
        if (value != null) {
            if (isSelected) {
                setOpaque(true);
                setBackground(Color.green);
            } else {
                setBackground(list.getBackgroundNonSelectionColor());
                setForeground(list.getForeground());
            }
            if (Camp.class.isInstance(value)) {
                Camp camp = (Camp) value;
                setText(camp.getNom().toUpperCase());
            } else if (Produccio.class.isInstance(value)) {
                Produccio produccio = (Produccio) value;
                setText(Configuracio.sdf.format(produccio.getDataPlanta()) + " " + produccio.getProductesId().getNom());
                if (!Utilitats.compleixPlazSeguretat(produccio)) {
                    setBackground(Color.ORANGE);
                }
            } else if (Collita.class.isInstance(value)) {
                Collita collita = (Collita) value;
                setText(Configuracio.sdf.format(collita.getData()) + " " + collita.getQuantitat() + " " + collita.getUnitatsMesuraId().getNom());
            } else if (Productor.class.isInstance(value)) {
                Productor productor = (Productor) value;
                setText(productor.getNom());
            } else if (Abonat.class.isInstance(value)) {
                Abonat abonat = (Abonat) value;
                setText(Configuracio.sdf.format(abonat.getDataAbonat()) + " " + abonat.getQuantitat() + abonat.getAbonoId().getUnitatsQuantitat().getNom() + " " + abonat.getAbonoId().getNom());
            } else if (Tractament.class.isInstance(value)) {
                Tractament tractament = (Tractament) value;
                setText(Configuracio.sdf.format(tractament.getDataTractament()) + " " + tractament.getFitosanitariId().getNomComercial() + "(" + tractament.getDosis() + tractament.getFitosanitariId().getUnitatsDosis().getNom() + "/" + tractament.getFitosanitariId().getUnitatsDisolucio().getNom() + ")");
            } else {
                setText(value.toString());
            }
        } else {
            setText("");
        }
        return this;
    }
}
